package com.gfdd.tea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gfdd.tea.common.R;
import com.gfdd.tea.pojo.ShoppingCart;
import com.gfdd.tea.service.ShoppingCartService;
import com.gfdd.tea.utlis.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
   @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 根据当前用户获取购物车数据
     * @param userId
     * @return
     */
   @GetMapping("/list")
   public R<List<ShoppingCart>> index(Long userId){
      // Long userId=new Long(1);
       LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
       wrapper.eq(ShoppingCart::getUserId,userId).orderByDesc(ShoppingCart::getCreateTime);
       List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
       return R.success(shoppingCartList);
   }

    /**
     * 根据茶叶id或者订单增加商品
     * 满足在首页添加和购物车列表添加
     * @param shoppingCart
     * @return
     */
   @PostMapping("/add")
   public R<String> add(@RequestBody  ShoppingCart shoppingCart,Long userId){
      // Long userId= new Long(1);
       LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
       wrapper.eq(ShoppingCart::getUserId,userId);
       wrapper.eq(shoppingCart.getTeaId()!=null,ShoppingCart::getTeaId,shoppingCart.getTeaId());
       wrapper.eq(shoppingCart.getId()!=null,ShoppingCart::getId,shoppingCart.getId());
       ShoppingCart cart = shoppingCartService.getOne(wrapper);
       if (cart!=null){
           //茶叶信息在购物车中 数量加1
           Integer number = cart.getNumber();
           cart.setNumber(number+1);
       try {
           shoppingCartService.updateById(cart);
       }catch (Exception e){
           e.printStackTrace();
           return R.error("添加失败！");
       }
   }else {
           //茶不在购物车中 添加到购物车
           shoppingCart.setCreateTime(DateUtils.DateFormat(new Date()));
           shoppingCartService.save(shoppingCart);
       }
       return R.success("添加成功");
   }
    /**
     * 购物车茶数量减 1
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart,Long userId){
        Long teaId=shoppingCart.getTeaId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();

       // Long userId=new Long(1);
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        queryWrapper.eq( ShoppingCart::getTeaId,teaId);
        ShoppingCart cartOne = shoppingCartService.getOne(queryWrapper);
        //判断菜品数量是否为1 为1删除 false减1
        if (cartOne.getNumber()==1){
            shoppingCartService.removeById(cartOne.getId());
        }else {
            Integer number = cartOne.getNumber();
            cartOne.setNumber(number-1);
            shoppingCartService.updateById(cartOne);
        }
        return R.success("减一成功");
    }
    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(Long userId){
        //Long userId=new Long(1);
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功");
    }
}
